import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import fire_component_msg from '@feidao/web/atom/browser/fsm/fire-component-msg';
import nodejs from "@feidao/web/atom/browser/msg/nodejs";
import upload from '../atom/upload';
import _hide from '../atom/hide';

export default async function a004(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
	//  保存
	const f = fd.data.node.querySelector<HTMLFormElement>('#certifi_info_form');
	const staff_number = fd.data.staff_number;
	if (!staff_number) {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '请选择员工');
		return;
	}
	const no = f.no.value;
	if (!no) {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '请选择证件类型');
		return;
	}
	const name = f.no.selectedOptions[0].innerText;
	const certificate = f.certificate.value;
	if (!certificate) {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '请输入证书名称');
		return;
	}
	const confirm_date = f.confirm_date.value;
	if (!confirm_date) {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '请选择取证日期');
		return;
	}
	const effectivedate = f.effectivedate.value;
	const staus = f.staus.value;
	if (!staus) {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '请选择状态');
		return;
	}
	const purchase_right = f.purchase_right.value;
	const enddate = f.enddate.value;
	const org_name = f.org_name.value;
	if (!org_name) {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '请输入发证机构');
		return;
	}
	//１、先上传文件
	if (!f.accessory_url.value) {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '请选择要上传的文件');
		return;
	}
	const accessory_id = fd.data.accessory_id;
	const res_upload: any = await upload(fd, accessory_id, f.accessory_url);
	if (res_upload == 'fail') {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '文件上传失败');
		return;
	} else if (res_upload.filename) {
		fd.data.accessory_url = res_upload.filename;
		// fire_component_msg('zj-common-002', 'ec-001', 'success', '上传成功');
	} else {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '文件上传失败');
		return;
	}
	//２、再保存数据
	const accessory_url = fd.data.accessory_url;
	const memo = f.memo.value;
	const res = await nodejs(fd.data.spaceid, 'certifi_info_add', { staff_number, no, name, certificate, confirm_date, effectivedate, staus, purchase_right, enddate, org_name, accessory_url, memo });
	if (res === 'success') {
		fire_component_msg('zj-common-002', 'ec-001', 'success', '保存成功');
		fire_component_msg('zj-qualification-001', 'ec-001');
		_hide(fd.data.node);
	} else {
		fire_component_msg('zj-common-002', 'ec-001', 'fail', '保存失败');
	}
}
